<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class manufacturer extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("mmanufacturer");
        $this->load->model("msoft");
        $this->load->model("mresource");
		
		$this->resCode = $this->mmanufacturer->resCode;
        $this->url_index = admin_url() . "manufacturer";
        $this->folder_upload = "dataupload/manufacturer/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index($page = 0) {
        $data = array();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = 20;
        $config['cur_page'] = $page;

        $config['base_url'] = $this->url_index;
        $config['first_url'] = $config['base_url'] . ".html";
        $config['total_rows'] = $this->mmanufacturer->num();
        $this->pagination->initialize($config);

        $this->db->limit($config['per_page'], ($page - 1 ) * $config['per_page']);
		$data['datas'] = $this->mmanufacturer->gets();
        $this->theme_data["main"] = "manufacturer/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($manID = 0) {
        $data = array();
        $post = $this->input->post();
        $data['man'] = $this->mmanufacturer->get($manID, 'MAN_ID');
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
			if($upload = $this->upload(@$data['man']->MAN_THUMB,@$data['man']->MAN_CODE)){
				$post['man']['MAN_THUMB'] = $this->folder_upload.$upload['upload_data']['file_name'];
			}
            if ($manID > 0) {
				
                $this->mmanufacturer->update($manID, 'MAN_ID', $post['man']);
            }
			else{
				$manID = $this->mmanufacturer->insert($post['man']);
			}
			
			//update cache soft
			$this->msoft->update_cache_man($manID);
            redirect($this->url_edit . $manID);
        }

        $this->theme_data["main"] = "manufacturer/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($manID = 0){
		if($manID > 0){
			$info = $this->mmanufacturer->get($manID, 'MAN_ID');
			if(file_exists('./'.$info->MAN_THUMB))
				unlink('./'.$info->MAN_THUMB);
			$this->mmanufacturer->delete($manID, 'MAN_ID');
		}
	}
	
	function upload($old_name = '', $newName = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['overwrite'] = true;
		if($newName && isset($newName) && $newName!='')
			$config['file_name']	= $newName.'-thumb';
			
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
			
		//$config['max_size']	= '10000';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';

		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
				unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
		
	}
	
	function ajax_search($type ='title'){
		$s = $this->input->post('s');
		 $this->db->like("MAN_NAME",$s);
		 $this->db->from("manufacturer");
		 $mans = $this->db->get()->result();
		 $arr_result = array();
		foreach($mans as $man){
			$t = array();
			$t['id'] = $man->MAN_ID;
			$t['title'] = $man->MAN_NAME;
			$arr_result[] = $t;
		}
		die(json_encode($arr_result));
	}
}
